package com.company.cloud.club.modules.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class LiveUtils {

    public static final String PUSH_KEY = "7c491e7d744fca676763efa2d892e15d";

    public static final String PLAYER_KEY = "ArtShanghai";

    public static String md5(String param) {
        if (param == null || param.length() == 0) {
            return null;
        }
        try {
            MessageDigest MD5 = MessageDigest.getInstance("MD5");
            MD5.update(param.getBytes());
            byte[] byteArray = MD5.digest();

            BigInteger bigInt = new BigInteger(1, byteArray);
            // 参数16表示16进制
            String result = bigInt.toString(16);
            // 不足32位高位补零
            while (result.length() < 32) {
                result = "0" + result;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成推流地址
     *
     * @param appName    推流AppName
     * @param streamName 推流StreamName
     * @param expireTime 过期时间（单位是秒）
     */
    public static String generatePushUrl(String appName, String streamName, long expireTime) {
        Long timeStamp = System.currentTimeMillis() / 1000L + expireTime;
        String txTime = String.format("%X", timeStamp);
        String stringToMd5 = PUSH_KEY + streamName + txTime;
        String authKey = md5(stringToMd5);
        return "rtmp://livepush.artshanghaifair.com/" + appName + "/" + streamName +
                "?txSecret=" + authKey + "&txTime=" + txTime;
    }

    /**
     * 生成播放地址
     *
     * @param appName    播放appName（同推流appName)
     * @param streamName 播放streamName (同推流streamName）
     * @param expireTime 过期时间（单位是秒
     */
    public static String generatePullRtmpUrl(String appName, String streamName, long expireTime) {
        Long timeStamp = System.currentTimeMillis() / 1000L + expireTime;
        String txTime = String.format("%X", timeStamp);
        String stringToMd5 = PLAYER_KEY + streamName + txTime;
        String authKey = md5(stringToMd5);
        return "rtmp://player.artshanghaifair.com/" + appName + "/" + streamName +
                "?txSecret=" + authKey + "&txTime=" + txTime;
    }

    /**
     * 生成
     *
     * @param appName
     * @param streamName
     * @param expireTime
     * @return
     */
    public static String generatePullHlsUrl(String appName, String streamName, long expireTime) {
        Long timeStamp = System.currentTimeMillis() / 1000L + expireTime;
        String txTime = String.format("%X", timeStamp);
        String stringToMd5 = PLAYER_KEY + streamName + txTime;
        String authKey = md5(stringToMd5);
        return "http://player.artshanghaifair.com/" + appName + "/" + streamName +
                ".m3u8?txSecret=" + authKey + "&txTime=" + txTime;
    }

    public static String generatePullFlvUrl(String appName, String streamName, long expireTime) {
        Long timeStamp = System.currentTimeMillis() / 1000L + expireTime;
        String txTime = String.format("%X", timeStamp);
        String stringToMd5 = PLAYER_KEY + streamName + txTime;
        String authKey = md5(stringToMd5);
        return "http://player.artshanghaifair.com/" + appName + "/" + streamName +
                ".flv?txSecret=" + authKey + "&txTime=" + txTime;
    }

}
